package com.distribute.framework.plugin;

import java.util.Date;
import java.util.List;

import java.lang.reflect.Method;

import com.distribute.framework.model.*;
import com.distribute.framework.core.lang.Message;
import com.distribute.framework.core.web.helper.Utils;


public class Event {

	public static void tips(Object from,Object to,Object data,String title,String content) {
		Notify.sendMessage("admin.comet", Message.create("tips", ""+getId(from), to==null?"*":(""+getId(to)), dataFlag(data),title, Message.ContentType.txt,content,getUrl(data)).addPayload("id", Utils.uuid()));
	}
	
	public static void notice(Object from,Object to,Object data,String title,String content) {		
		Notify.sendMessage("admin.comet", Message.create("notice", ""+getId(from), to==null?"*":(""+getId(to)), dataFlag(data),title, Message.ContentType.txt,content,getUrl(data)).addPayload("id", Utils.uuid()));
	}
	
	public static void toast(Object from,Object to,Object data,String title,String content) {		
		Notify.sendMessage("admin.comet", Message.create("toast", ""+getId(from), to==null?"*":(""+getId(to)), dataFlag(data),title, Message.ContentType.txt,content,getUrl(data)).addPayload("id", Utils.uuid()));
	}
	
	public static void alert(Object from,Object to,Object data,String title,String content) {		
		Notify.sendMessage("admin.comet", Message.create("alert", ""+getId(from), to==null?"*":(""+getId(to)), dataFlag(data),title, Message.ContentType.txt,content,getUrl(data)).addPayload("id", Utils.uuid()));
	}
	
	private static String dataFlag(Object data) {
		if(data!=null){
			return data.getClass().getSimpleName()+"@"+getId(data);
		}
		return null;
	}
	private static String getId(Object obj) {
		if(obj==null)return "*";
		try{
			Method method = obj.getClass().getMethod("getId");
			method.setAccessible(true);
			return "" + method.invoke(obj);
		}catch(Exception ex){
			ex.printStackTrace();
			return "*";
		}
	}
	private static String getUrl(Object data) {
		if(data==null)return null;
		try{
			Method method = data.getClass().getMethod("getDetailUrl");
			method.setAccessible(true);
			return "" + method.invoke(data);
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

}